package com.algebra.active.controller;

import cn.hutool.json.JSONUtil;
import com.algebra.active.service.IBaseWorkflowService;
import com.algebra.active.service.INodeOprService;
import com.algebra.active.util.ServiceApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author al
 * @date 2020/2/21 10:57
 * @description 工作流 v2.0 服务接口
 */
@Slf4j
@RestController
@Api(value = "工作流 v2.0 服务接口", tags = "工作流 v2.0 服务接口")
public class BaseWorkflowController {

    @Autowired
    IBaseWorkflowService baseWorkflowService;

    @Autowired
    INodeOprService nodeOprService;

    @PostMapping("/initWorkflow")
    @ApiOperation("初始化流程")
    public ServiceApiResult<Map<String, Object>> initWorkflow(@RequestBody Map<String, Object> param) {
        log.info("[工作流初始化流程]接受到参数：\n {}", JSONUtil.toJsonStr(param));
        ServiceApiResult<Map<String, Object>> result = new ServiceApiResult<>();
        try {
            if(param == null){
                result.setSuccess(false);
                result.setMessage("初始化工作流参数为空，请检查请求参数！");
                result.setErrorMessage("初始化工作流参数为空，请检查请求参数！");
                return result;
            }
            return baseWorkflowService.initWorkflow(param);
        } catch (Exception e){
            e.printStackTrace();
            log.error("初始化工作流接口调用异常，\n exp-msg:{}, \n stack-trace:{}",e.getMessage(),e.getStackTrace());
            result.setMessage("初始化工作流接口调用异常, exp-msg:"+e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("/completeMyTask")
    @ApiOperation("推动工作流节点")
    public ServiceApiResult<Map<String, Object>> completeMyTask(@RequestBody Map<String, Object> param){
        log.info("[推动工作流节点]接受到参数：\n {}", JSONUtil.toJsonStr(param));
        ServiceApiResult<Map<String, Object>> result = new ServiceApiResult<>();
        try {
            if(param == null){
                result.setSuccess(false);
                result.setMessage("推动工作流参数为空，请检查请求参数！");
                result.setErrorMessage("推动工作流参数为空，请检查请求参数！");
                return result;
            }
            return baseWorkflowService.completeMyTask(param);
        } catch (Exception e){
            e.printStackTrace();
            log.error("推动工作流节点接口调用异常，\n exp-msg:{}, \n stack-trace:{}",e.getMessage(),e.getStackTrace());
            result.setMessage("推动工作流节点接口调用异常, exp-msg:"+e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }


}
